package org.ssc.tickethouse.controller

import org.junit.Ignore
import org.ssc.tickethouse.entity.Audience
import spock.lang.See
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Title
import spock.lang.Unroll
import org.ssc.tickethouse.repository.AudienceRepository

/**
 * Spock单元测试示例
 */
@Title("This is an example for Spock unit test")
@See("http://spockframework.org/spec")
class TickethouseHandlerUnitSpec extends Specification {

    @Shared
    def tickethouseHandler = new TickethouseHandler()

    @Ignore
    def "Simple unit test example"() {
        when: "Verify phone number"
        def result = tickethouseHandler.validPhone("123456")
        then: "Verify result"
        !result
    }

    @Unroll
    def "Parameterized Test: #phoneNum"() {
        expect: "Verify a list of phone number"
        result == tickethouseHandler.validPhone(phoneNum)
        where: "Test data"
        phoneNum        || result
        "15661235735"   || true
        "1234656"       || false
        "abcdefghijk"   || false
        "1234567891011" || false
        ""              || false
    }

    def "Registration for a new Audience"() {
        given: "I am a new Audience with valid parameters"
            def audience = new Audience(aname: "foo", password: "123456", phone: "15661235735", age: 60, email: "abc@def.org")
        and: "Audience 'foo' should not be existed"
            def mockAudienceRepository = Mock(AudienceRepository) {
                //findAudience方法，输入为'foo'，返回null
                findAudience("foo") >> null
                //audienceRegister方法，输入为audience，返回1
                audienceRegister(audience) >> 1
            }
            tickethouseHandler.setAudienceRepository(mockAudienceRepository)
        when: "I register the Audience 'foo'"
            def result = tickethouseHandler.userRegister(audience)
        then: "The result should be 1"
            result == 1
        and: "AudienceRepository should be called once"
            with(mockAudienceRepository) {
                //audienceRegister方法被调用1次
                1 * audienceRegister(_) >> 1
                //mockAudienceRepository没有其他交互
                0 * _
            }
    }


}
